
package com.yimixian.app.rest.api;

import com.yimixian.app.address.ShortAddress;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.Category;
import com.yimixian.app.model.SearchKeyResult;
import com.yimixian.app.model.Section;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by songzy on 2015/10/28.
 */
public class CategoryAPI extends YmxBaseAPI<Category> {
    public final static String CATEGORY_GET = "/v5/categories";

    public String addressId;
    public String poiId;
    public String longitude;
    public String latitude;
    public String coordSystem;

    /**
     * @param callback
     */
    public CategoryAPI(Callback<Response> callback) {
        super(callback);
    }

    public void categoryGet() {
        Address address = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        if (address != null && address instanceof ShortAddress) {
            poiId = address.poiId;
            longitude = address.longitude;
            latitude = address.latitude;
            coordSystem = address.coordSystem;
            mYmxRestService.categoryGet(poiId, longitude, latitude, coordSystem,token, mCallback);
        } else if(address != null && address instanceof Address){
            addressId = String.valueOf(address.id);
            mYmxRestService.categoryGet(addressId,token, mCallback);
        } else {
            UiUtils.showToast("没有地址");
            EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_DISSMISS_DIALOG));
        }
    }

    @Override
    public String getRelativeURL() {
        return CATEGORY_GET;
    }

    @Override
    public Category parseJson(JSONObject jsonObject) {
        Category category = new Category();
        List<Section> categories = new ArrayList<>();
//        List<GoodsItem> goods = new ArrayList<>();
//        List<Banner> banners = new ArrayList<>();
        if (jsonObject != null) {
            JSONArray categoryArray = jsonObject.optJSONArray("categories");
            if (categoryArray != null) {
                for (int i = 0; i < categoryArray.length(); i++) {
                    JSONObject categoryObj = categoryArray.optJSONObject(i);
                    if (categoryObj != null) {
                        int id = categoryObj.optInt("id");
                        String name = categoryObj.optString("name");
                        String subName = categoryObj.optString("sub_name");
                        Section section = new Section(id, name, subName, 0, null, null, null);
                        categories.add(section);
                    }
                }
            }
            category.categories = categories;
//            category.categoryId = jsonObject.optInt("category_id");
//            JSONArray goodsArray = jsonObject.optJSONArray("goods");
//            if (goodsArray != null) {
//                for (int i = 0; i < goodsArray.length(); i++) {
//                    JSONObject goodObj = goodsArray.optJSONObject(i);
//                    if (goodObj != null) {
//                        GoodsItem goodsItem = new GoodsItem();
//                        goodsItem.id = goodObj.optInt("id");
//                        goodsItem.name = goodObj.optString("name");
//                        goodsItem.imgRatio = goodObj.optInt("img_ratio");
//                        goodsItem.img = goodObj.optString("img");
//                        goodsItem.promotionImg = goodObj.optString("promotion_img");
//                        goodsItem.descUrl = goodObj.optString("desc_url");
//                        goodsItem.unitDesc = goodObj.optString("unit_desc");
//                        goodsItem.saleStatus = goodObj.optString("sale_status");
//                        goodsItem.stockCount = goodObj.optString("stock_count");
//                        goodsItem.saleStatus = goodObj.optString("sale_status");
//                        goodsItem.unitPrice = goodObj.optString("unit_price");
//                        goodsItem.unitOldPrice = goodObj.optString("unit_old_price");
//                        goodsItem.shouldBuyCount = goodObj.optInt("should_buy_count");
//                        goodsItem.isBuyingGroup = goodObj.optInt("is_buying_group");
//                        goodsItem.buyingGroupCount = goodObj.optInt("buying_group_count");
//                        goodsItem.buyingGroupPrice = (float) goodObj
//                                .optDouble("buying_group_price");
//                        goodsItem.countZeroReason = goodObj.optString("count_zero_reason");
//                        goodsItem.start_time = goodObj.optLong("start_time");
//                        goods.add(goodsItem);
//                    }
//                }
//            }
//            category.goods = goods;
//            JSONArray bannersArray = jsonObject.optJSONArray("banners");
//            if (bannersArray != null) {
//                for (int i = 0; i < bannersArray.length(); i++) {
//                    JSONObject bannerObj = bannersArray.optJSONObject(i);
//                    if (bannerObj != null) {
//                        String image = bannerObj.optString("image");
//                        String url = bannerObj.optString("url");
//                        Banner banner = new Banner(image, url);
//                        banners.add(banner);
//                    }
//                }
//            }
//            category.banners = banners;
//            category.bannersImageRatio = jsonObject.optInt("banners_image_ratio");
            category.storeId = jsonObject.optInt("store_id");
            category.cannotDeliverText = jsonObject.optString("cannot_deliver_text");
            category.deliverZoneUrl = jsonObject.optString("delivery_zone_url");
        }
        return category;
    }
}
